export interface YinyuanSymbols {
  [key: string]: string;
}

export interface PatternRules {
  [key: string]: string[];
}

export interface EncodingRules {
  general_pattern: {
    [key: string]: string;
  };
}

export interface ToneEncodings {
  [key: string]: string;
}

export interface SymbolsUsed {
  high: string;
  mid: string;
  low: string;
}

export interface PostLongSymbolsUsed {
  initial: SymbolsUsed;
  main_vowel: SymbolsUsed;
}

export interface FinalEncoding {
  base: string;
  display: string[];
  encodings: ToneEncodings;
  symbols_used: SymbolsUsed | PostLongSymbolsUsed;
}

export interface GanyinEncoding {
  single_quality_finals: {
    [key: string]: FinalEncoding;
  };
  post_long_finals: {
    [key: string]: FinalEncoding;
  };
}

export interface GanyinData {
  description: string;
  encoding_rules: EncodingRules;
  yinyuan_symbols: YinyuanSymbols;
  pattern_rules: PatternRules;
  notes: string;
  ganyin_encoding: GanyinEncoding;
}

// 干音编码系统数据
export const ganyinData: GanyinData = {
  "description": "干音编码系统。干音是由声调与韵母构成的音段的统称，俗名带调韵母。这套编码系统使用三个音元组合表示不同干音。在现代通用汉语中，音元特指音高特征或音质特征不同的短音，是语音的基本结构单元。",
  "encoding_rules": {
    "general_pattern": {
      "first_tone_ganyin": "三个高调音元的组合",
      "second_tone_ganyin": "低调音元、中调音元、高调音元的组合",
      "third_tone_ganyin": "三个低调音元的组合",
      "fourth_tone_ganyin": "高调音元、中调音元、低调音元的组合"
    }
  },
  "yinyuan_symbols": {
    "high_tone_i": "󰌠",
    "mid_tone_i": "󰌡",
    "low_tone_i": "󰌤",
    "high_tone_u": "󰌪",
    "mid_tone_u": "󰌫",
    "low_tone_u": "󰌮",
    "high_tone_ü": "󰌴",
    "mid_tone_ü": "󰌵",
    "low_tone_ü": "󰌸",
    "high_tone_a": "󰌾",
    "mid_tone_a": "󰌿",
    "low_tone_a": "󰍂",
    "high_tone_o": "󰍒",
    "mid_tone_o": "󰍓",
    "low_tone_o": "󰍖",
    "high_tone_e": "󰍡",
    "mid_tone_e": "󰍢",
    "low_tone_e": "󰍥",
    "high_tone_-i": "󰍵",
    "mid_tone_-i": "󰍶",
    "low_tone_-i": "󰍹",
    "high_tone_er": "󰎄",
    "mid_tone_er": "󰎅",
    "low_tone_er": "󰎈",
    "high_tone_m": "󰎎",
    "mid_tone_m": "󰎏",
    "low_tone_m": "󰎒",
    "high_tone_n": "󰎓",
    "mid_tone_n": "󰎔",
    "low_tone_n": "󰎗",
    "high_tone_ng": "󰎘",
    "mid_tone_ng": "󰎙",
    "low_tone_ng": "󰎜"
  },
  "pattern_rules": {
    "first_tone": ["high", "high", "high"],
    "second_tone": ["low", "mid", "high"],
    "third_tone": ["low", "low", "low"],
    "fourth_tone": ["high", "mid", "low"]
  },
  "notes": "此编码系统暂不扩展到其他干音。其它干音的编码遵循不同模式规则。",
  "ganyin_encoding": {
    "single_quality_finals": {
      "i": {
        "base": "i",
        "display": ["ī", "í", "ǐ", "ì"],
        "encodings": {
          "ī": "󰌠󰌠󰌠",
          "í": "󰌤󰌡󰌠",
          "ǐ": "󰌤󰌤󰌤",
          "ì": "󰌠󰌡󰌤"
        },
        "symbols_used": {
          "high": "󰌠",
          "mid": "󰌡",
          "low": "󰌤"
        }
      },
      "u": {
        "base": "u",
        "display": ["ū", "ú", "ǔ", "ù"],
        "encodings": {
          "ū": "󰌪󰌪󰌪",
          "ú": "󰌮󰌫󰌪",
          "ǔ": "󰌮󰌮󰌮",
          "ù": "󰌪󰌫󰌮"
        },
        "symbols_used": {
          "high": "󰌪",
          "mid": "󰌫",
          "low": "󰌮"
        }
      },
      "ü": {
        "base": "ü",
        "display": ["ǖ", "ǘ", "ǚ", "ǜ"],
        "encodings": {
          "ǖ": "󰌴󰌴󰌴",
          "ǘ": "󰌸󰌵󰌴",
          "ǚ": "󰌸󰌸󰌸",
          "ǜ": "󰌴󰌵󰌸"
        },
        "symbols_used": {
          "high": "󰌴",
          "mid": "󰌵",
          "low": "󰌸"
        }
      },
      "a": {
        "base": "a",
        "display": ["ā", "á", "ǎ", "à"],
        "encodings": {
          "ā": "󰌾󰌾󰌾",
          "á": "󰍂󰌿󰌾",
          "ǎ": "󰍂󰍂󰍂",
          "à": "󰌾󰌿󰍂"
        },
        "symbols_used": {
          "high": "󰌾",
          "mid": "󰌿",
          "low": "󰍂"
        }
      },
      "o": {
        "base": "o",
        "display": ["ō", "ó", "ǒ", "ò"],
        "encodings": {
          "ō": "󰍒󰍒󰍒",
          "ó": "󰍖󰍓󰍒",
          "ǒ": "󰍖󰍖󰍖",
          "ò": "󰍒󰍓󰍖"
        },
        "symbols_used": {
          "high": "󰍒",
          "mid": "󰍓",
          "low": "󰍖"
        }
      },
      "e": {
        "base": "e",
        "display": ["ē", "é", "ě", "è"],
        "encodings": {
          "ē": "󰍡󰍡󰍡",
          "é": "󰍥󰍢󰍡",
          "ě": "󰍥󰍥󰍥",
          "è": "󰍡󰍢󰍥"
        },
        "symbols_used": {
          "high": "󰍡",
          "mid": "󰍢",
          "low": "󰍥"
        }
      },
      "-i": {
        "base": "-i",
        "display": ["-ī", "-í", "-ǐ", "-ì"],
        "encodings": {
          "-ī": "󰍵󰍵󰍵",
          "-í": "󰍹󰍶󰍵",
          "-ǐ": "󰍹󰍹󰍹",
          "-ì": "󰍵󰍶󰍹"
        },
        "symbols_used": {
          "high": "󰍵",
          "mid": "󰍶",
          "low": "󰍹"
        }
      },
      "er": {
        "base": "er",
        "display": ["ēr", "ér", "ěr", "èr"],
        "encodings": {
          "ēr": "󰎄󰎄󰎄",
          "ér": "󰎈󰎅󰎄",
          "ěr": "󰎈󰎈󰎈",
          "èr": "󰎄󰎅󰎈"
        },
        "symbols_used": {
          "high": "󰎄",
          "mid": "󰎅",
          "low": "󰎈"
        }
      },
      "m": {
        "base": "m",
        "display": ["m̄", "ḿ", "m̌", "m̀"],
        "encodings": {
          "ḿ": "󰎎󰎎󰎎",
          "m̄": "󰎒󰎏󰎎",
          "m̌": "󰎒󰎒󰎒",
          "m̀": "󰎎󰎏󰎒"
        },
        "symbols_used": {
          "high": "󰎎",
          "mid": "󰎏",
          "low": "󰎒"
        }
      },
      "n": {
        "base": "n",
        "display": ["n̄", "ń", "ň", "ǹ"],
        "encodings": {
          "n̄": "󰎓󰎓󰎓",
          "ń": "󰎗󰎔󰎓",
          "ň": "󰎗󰎗󰎗",
          "ǹ": "󰎓󰎔󰎗",
        },
        "symbols_used": {
          "high": "󰎓",
          "mid": "󰎔",
          "low": "󰎗"
        }
      },
      "ng": {
        "base": "ng",
        "display": ["n̄g", "ńg", "ňg", "ǹg"],
        "encodings": {
          "n̄g": "󰎘󰎘󰎘",
          "ńg": "󰎜󰎙󰎘",
          "ňg": "󰎜󰎜󰎜",
          "ǹg": "󰎘󰎙󰎜"
        },
        "symbols_used": {
          "high": "󰎘",
          "mid": "󰎙",
          "low": "󰎜"
        }
      }
    },
    "post_long_finals": {
      "ia": {
        "base": "ia",
        "display": ["iā", "iá", "iǎ", "ià"],
        "encodings": {
          "iā": "󰌠󰌾󰌾",
          "iá": "󰌤󰌿󰌾",
          "iǎ": "󰌤󰍂󰍂",
          "ià": "󰌠󰌿󰍂"
        },
        "symbols_used": {
          "initial": {
            "high": "󰌠",
            "mid": "󰌡",
            "low": "󰌤"
          },
          "main_vowel": {
            "high": "󰌾",
            "mid": "󰌿",
            "low": "󰍂"
          }
        }
      },
      "ua": {
        "base": "ua",
        "display": ["uā", "uá", "uǎ", "uà"],
        "encodings": {
          "uā": "󰌪󰌾󰌾",
          "uá": "󰌮󰌿󰌾",
          "uǎ": "󰌮󰍂󰍂",
          "uà": "󰌪󰌿󰍂"
        },
        "symbols_used": {
          "initial": {
            "high": "󰌪",
            "mid": "󰌫",
            "low": "󰌮"
          },
          "main_vowel": {
            "high": "󰌾",
            "mid": "󰌿",
            "low": "󰍂"
          }
        }
      },
      "io": {
        "base": "io",
        "display": ["iō", "ió", "iǒ", "iò"],
        "encodings": {
          "iō": "󰌠󰍒󰍒",
          "ió": "󰌤󰍓󰍒",
          "iǒ": "󰌤󰍖󰍖",
          "iò": "󰌠󰍓󰍖"
        },
        "symbols_used": {
          "initial": {
            "high": "󰌠",
            "mid": "󰌡",
            "low": "󰌤"
          },
          "main_vowel": {
            "high": "󰍒",
            "mid": "󰍓",
            "low": "󰍖"
          }
        }
      },
      "uo": {
        "base": "uo",
        "display": ["uō", "uó", "uǒ", "uò"],
        "encodings": {
          "uō": "󰌪󰍒󰍒",
          "uó": "󰌮󰍓󰍒",
          "uǒ": "󰌮󰍖󰍖",
          "uò": "󰌪󰍓󰍖"
        },
        "symbols_used": {
          "initial": {
            "high": "󰌪",
            "mid": "󰌫",
            "low": "󰌮"
          },
          "main_vowel": {
            "high": "󰍒",
            "mid": "󰍓",
            "low": "󰍖"
          }
        }
      },
      "ie": {
        "base": "ie",
        "display": ["iē", "ié", "iě", "iè"],
        "encodings": {
          "iē": "󰌠󰍡󰍡",
          "ié": "󰌤󰍢󰍡",
          "iě": "󰌤󰍥󰍥",
          "iè": "󰌠󰍢󰍥"
        },
        "symbols_used": {
          "initial": {
            "high": "󰌠",
            "mid": "󰌡",
            "low": "󰌤"
          },
          "main_vowel": {
            "high": "󰍡",
            "mid": "󰍢",
            "low": "󰍥"
          }
        }
      },
      "üe": {
        "base": "üe",
        "display": ["üē", "üé", "üě", "üè"],
        "encodings": {
          "üē": "󰌴󰍡󰍡",
          "üé": "󰌸󰍢󰍡",
          "üě": "󰌸󰍥󰍥",
          "üè": "󰌴󰍢󰍥"
        },
        "symbols_used": {
          "initial": {
            "high": "󰌴",
            "mid": "󰌵",
            "low": "󰌸"
          },
          "main_vowel": {
            "high": "󰍡",
            "mid": "󰍢",
            "low": "󰍥"
          }
        }
      }
    }
  }
};

// 获取所有单质韵母
export const getSingleQualityFinals = () => {
  return Object.keys(ganyinData.ganyin_encoding.single_quality_finals);
};

// 获取所有后长韵母
export const getPostLongFinals = () => {
  return Object.keys(ganyinData.ganyin_encoding.post_long_finals);
};

// 获取所有韵母
export const getAllFinals = () => {
  return [...getSingleQualityFinals(), ...getPostLongFinals()];
};

// 获取特定韵母的编码信息
export const getFinalEncoding = (final: string) => {
  if (ganyinData.ganyin_encoding.single_quality_finals[final]) {
    return ganyinData.ganyin_encoding.single_quality_finals[final];
  }
  if (ganyinData.ganyin_encoding.post_long_finals[final]) {
    return ganyinData.ganyin_encoding.post_long_finals[final];
  }
  return null;
};

// 获取特定声调的模式规则
export const getTonePattern = (tone: number) => {
  const toneKey = `${getToneName(tone)}_tone`;
  return ganyinData.pattern_rules[toneKey] || [];
};

// 获取声调名称
export const getToneName = (tone: number) => {
  switch (tone) {
    case 1: return "first";
    case 2: return "second";
    case 3: return "third";
    case 4: return "fourth";
    default: return "unknown";
  }
};

// 获取声调的中文名称
export const getToneChineseName = (tone: number) => {
  switch (tone) {
    case 1: return "阴平";
    case 2: return "阳平";
    case 3: return "上声";
    case 4: return "去声";
    default: return "未知";
  }
};